package com.wendu.oa.service.security.impl;

import com.wendu.oa.dao.security.HomeDao;
import com.wendu.oa.dto.MenuDto;
import com.wendu.oa.model.Fun;
import com.wendu.oa.service.security.HomeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@Transactional
public class HomeServiceImpl implements HomeService {

    @Resource
    private HomeDao homeDao;

    @Override
    public List<MenuDto> getMenuList(String uid) {

        List<MenuDto> mainList = new ArrayList<>();

        List<Fun> funList = homeDao.findMenuList(uid);

        MenuDto current = null;//当前主菜单

        for(Fun fun : funList){
            if(current == null || !current.getMenuId().equals(fun.getP_id())){
                current = new MenuDto();
                mainList.add(current);
                current.setMenuId(fun.getP_id());
                current.setMenuName(fun.getP_name());
                current.setChildren(new ArrayList<MenuDto>());
            }
            MenuDto sub = new MenuDto();
            sub.setMenuId(fun.getC_id());
            sub.setMenuName(fun.getC_name());
            sub.setMenuUrl(fun.getUrl());
            current.getChildren().add(sub);

        }

        return mainList;

    }
}
